package kz.kstu.shop.desktop.ui.form;

import kz.kstu.shop.app.delegate.api.GuideDelegate;
import kz.kstu.shop.app.delegate.impl.GuideDelegateImpl;
import kz.kstu.shop.business.entity.guide.Product;
import kz.kstu.shop.desktop.api.SimpleTableModal;

import javax.swing.*;
import java.util.List;

/**
 * @author Filipp_Stankevich
 *         created: 7/25/11 5:43 PM
 */
public class ProductGuidePage {
    private JPanel rootPanel;
    private JTable table;
    private SimpleTableModal tableModal;

    public JPanel getRootPanel() {
        return rootPanel;
    }

    public void initStaticComponent() {
        tableModal = new SimpleTableModal("Код","Штрих-код","Наименование");
        table.setModel(tableModal);
        table.getTableHeader().setReorderingAllowed(false);
    }

    public void initData() {
        GuideDelegate delegate = new GuideDelegateImpl();
        List<Product> products = delegate.findAllProducts();
        updateTable(products);
    }

    private void updateTable(List<Product> products) {
        for (Product product : products) {
            Object[] row = createRow(product);
            tableModal.addRow(row);
        }
    }

    private Object[] createRow(Product product){
       return new Object[] {product.getId(), product.getBarCode(), product.getName()};
    }
}
